<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>调用state定时刷新</title>
  <script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>
  <script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>
  <script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
</body>
<script type="text/babel">
 class Clock extends React.Component {
   constructor (props){
     super(props);
     this.state = {
       date:new Date().toLocaleString()
     };

   }
   componentDidMount(){
     this.timerID = setInterval(
       () => this.tick(),
       1000
     );
   }

   tick(){
     this.setState({
       date:new Date()
     });
   }



   componentWillUnmount(){
     clearInterval(this.timerID)
   }

   render(){
     const element =<div><h1>{this.state.date.toLocaleString()}</h1></div>
     return element;
   }
 }
 function render() {
   ReactDOM.render(<div><Clock/></div>, document.getElementById('root'));
 }
 render();
</script>
</html>
